<template>
  <view class="work-container">
    <!-- 宫格组件 -->
    <uni-section title="常用功能" type="line"></uni-section>
    <view class="grid-body">
      <uni-grid :column="4" :showBorder="false" @change="changeGrid($event.detail.index,-1)">
        <uni-grid-item v-for="(item,index) in worklist" :key="index">
          <view class="grid-item-box">
            <svg-icon :iconClass="item.icon" size="30"/>
            <text class="text">{{item.title}}</text>
          </view>
        </uni-grid-item>
        <uni-grid-item :index="-1">
          <view class="grid-item-box">
            <uni-icons type="plusempty" size="30"></uni-icons>
          </view>
        </uni-grid-item>
      </uni-grid>
    </view>

    <view v-for="(section,idx) in routers" :key="idx">
      <!-- 宫格组件 -->
      <uni-section :title="section.title" type="line"></uni-section>
      <view class="grid-body">
        <uni-grid :column="4" :showBorder="false" @change="changeGrid($event.detail.index,idx)">
          <uni-grid-item v-for="(item,index) in section.children" :key="index" :index="index">
            <view class="grid-item-box">
              <svg-icon :iconClass="item.icon" size="30"/>
              <text class="text">{{item.title}}</text>
            </view>
          </uni-grid-item>
        </uni-grid>
      </view>
    </view>
  </view>
</template>

<script>
  import {
    getRouters
  } from '@/api/login'
  export default {
    data() {
      return {
        worklist: [],
        routers: [],
      }
    },
    onLoad() {
      this.loadData();
    },
    methods: {
      loadData(){
        this.$modal.loading('加载中...')
        this.worklist = this.$store.state.workbench.workbench;
        this.$store.dispatch('GetRouters').then(data => {
          this.routers = data;
          this.$modal.closeLoading()
        }).catch(e => {
          this.$modal.closeLoading()
        })
      },
      changeGrid(index,idx=0) {
        let work;
        if (idx<0) {
          work = this.worklist[index]??{path:'/pages/work/set'};
        }else{
          work = this.routers[idx].children[index];
        }
        // 链接
        if (work.link) {
          // #ifdef MP
          uni.setClipboardData({
            data:work.link,
            success() {
              uni.showModal({
                title:'由于小程序限制，已为您复制链接，请到浏览器中打开',
                showCancel:false,
                confirmText:'好的'
              })
            }
          })
          // #endif
          // #ifndef MP
          window.open(work.link)
          // #endif
          return;
        }
        // 页面
        if (work.path) {
          uni.navigateTo({
            url: work.path,
            fail:e=>{
              console.log(e.errMsg)
              this.$modal.showToast('模块建设中~')
            }
          })
        }
      }
    }
  }
</script>

<style lang="scss">
  /* #ifndef APP-NVUE */
  page {
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    background-color: #fff;
    min-height: 100%;
    height: auto;
  }

  view {
    font-size: 14px;
    line-height: inherit;
  }

  /* #endif */

  .text {
    text-align: center;
    font-size: 26rpx;
    margin-top: 10rpx;
  }

  .grid-item-box {
    flex: 1;
    /* #ifndef APP-NVUE */
    display: flex;
    /* #endif */
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 15px 0;
  }

  .uni-margin-wrap {
    width: 690rpx;
    width: 100%;
    ;
  }
</style>